/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.confsummary;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.account.SysUser;
import com.unis.model.attach.AttachLink;
import com.unis.model.confsummary.ConfSummary;
import com.unis.service.attach.AttachLinkService;

/**
 * @author Alucard (email:357375084@qq.com)
 * @since 2013-03-25
 */
@Service("confsummaryService")
public class ConfSummaryServiceImpl implements ConfSummaryService {

    private IDataSet4iBatis iDataSet4iBatis;
    private AttachLinkService attachlinkService;

    /****************************************
     * Insert a instace of ConfSummary
     ****************************************/
    /* (non-Javadoc)
     * @see com.unis.service.confsummary.ConfSummaryService#insert(com.unis.model.confsummary.ConfSummary)
     */
    public void insert(ConfSummary confsummary) throws Exception {
        confsummary.setSumid(TemplateUtil.genUUID());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");        
        
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        //为纪要创建者和创建时间赋值
        confsummary.setCreator(user.getUsername());
        confsummary.setCreatetime(sdf.format(new Date()));
        
        //为附件连接表添加数据
        if(confsummary.getFileid()!=null && !"".equals(confsummary.getFileid().trim())){
            AttachLink attachlink=new AttachLink();
            attachlink.setStatmentId("AttachLink.insert");
            String[] fileids=confsummary.getFileid().split(",");
            for(int i=0 ; i<fileids.length ; i++){                
                attachlink.setFileid(fileids[i]);
                attachlink.setEntityid(confsummary.getSumid());
                attachlinkService.insert(attachlink);
            }
        }
        iDataSet4iBatis.insert(confsummary);
    }

    /****************************************
     * Update a instace of ConfSummary
     ****************************************/
    public void update(ConfSummary confsummary) throws Exception {
      //为附件连接表添加数据
        if(confsummary.getFileid()!=null && !"".equals(confsummary.getFileid().trim())){
            AttachLink attachlink=new AttachLink();
            attachlink.setStatmentId("AttachLink.insert");
            String[] fileids=confsummary.getFileid().split(",");
            for(int i=0 ; i<fileids.length ; i++){                
                attachlink.setFileid(fileids[i]);
                attachlink.setEntityid(confsummary.getSumid());
                attachlinkService.insert(attachlink);
            }
        }
        iDataSet4iBatis.update(confsummary);
    }

    /****************************************
     * Delete a instace of ConfSummary
     ****************************************/
    public void delete(ConfSummary confsummary) throws Exception {
        iDataSet4iBatis.delete(confsummary);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(ConfSummary confsummary) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(ConfSummary confsummary) throws Exception {
        String statementId = confsummary.getStatmentId();
        String mainId = confsummary.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> confsummaryList = new ArrayList<BaseModel>();
        for (String id : ids) {
            ConfSummary model = new ConfSummary();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            confsummaryList.add(model);
        }
        iDataSet4iBatis.batchDelete(confsummaryList);
    }

    /****************************************
     * Query instaces of ConfSummary
     ****************************************/
    public List<BaseModel> queryAsList(ConfSummary confsummary) throws Exception {
        String queryListStatement = "";
        if (confsummary.isPaging()) {
            queryListStatement = confsummary.getStatmentId();
            confsummary.setStatmentId("ConfSummary.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(confsummary);
            confsummary.setTotalCount(baseModel.getTotalCount());
            confsummary.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(confsummary);
    }

    /****************************************
     * Query a instaces of ConfSummary
     ****************************************/
    public BaseModel queryAsObject(ConfSummary confsummary) throws Exception {
        return iDataSet4iBatis.queryAsObject(confsummary);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
    
    @Resource
    public void setAttachlinkService(AttachLinkService attachlinkService) {
        this.attachlinkService = attachlinkService;
    } 
}
